#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

static vector<int> prime_factorization(int n) {
    vector<int> factors;
    for (int i = 2; i * i <= n; ++i) {
        if (n % i == 0)
            factors.push_back(i);
        while (n % i == 0)
            n /= i;
    }
    if (n > 1)
        factors.push_back(n);
    return factors;
}

static int phi_function(int num) {
    vector<int> factors = prime_factorization(num);
    int phi = num;
    for (auto factor : factors)
        phi = phi / factor * (factor - 1);
    return phi;
}

static bool permutation(int a, int b) {
    auto fuck = [](int n){
        vector<int> v;
        while (n) {
            v.push_back(n % 10);
            n /= 10;
        }
        sort(v.begin(), v.end());
        return v;
    };
    auto v1 = fuck(a);
    auto v2 = fuck(b);
    for (int i = 0; i < v1.size(); ++i) {
        if (v1[i] != v2[i])
            return false;
    }
    return true;
}

int main() {
    float min = 87109 / 79180.0;
    for (int num = 2; num <= 10'000'000; ++num) {
        int phi = phi_function(num);
        double tmp = num * 1.0 / phi;
        if (tmp < min && permutation(num, phi)) {
            min = tmp;
            cout << num << endl;
        }
    }   // 8319823
}